package cn.edu.hust.cm.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kingcmchen on 2016/12/16.
 */

public class GroupGlideImageView extends HorizontalScrollView {

    private int mItemWidth = 96;
    private int mItemHeight = 96;

    private LinearLayout mContainer;

    private GlideImageView.GlideImageViewFactory mGlideImageViewFactory = GlideImageView.GlideImageViewFactory.getInstance();

    public GroupGlideImageView(Context context) {
        super(context);
    }

    public GroupGlideImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GroupGlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public GroupGlideImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.GroupGlideImageView, defStyleAttr, defStyleRes);
        mItemWidth = a.getDimensionPixelSize(R.styleable.GroupGlideImageView_cm_item_width, mItemWidth);
        mItemHeight = a.getDimensionPixelSize(R.styleable.GroupGlideImageView_cm_item_height, mItemHeight);
        a.recycle();

        LinearLayout container = new LinearLayout(getContext());
        container.setOrientation(LinearLayout.HORIZONTAL);
        container.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);

        GradientDrawable gd = new GradientDrawable();
        gd.setShape(GradientDrawable.RECTANGLE);
        // TODO: 2016/12/16 hard-code
        gd.setSize(18, 0);

        gd.setColor(ContextCompat.getColor(getContext(), android.R.color.transparent));

        container.setDividerDrawable(gd);
        addView(container, new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mContainer = container;
    }

    /**
     * Specify a GlideImageViewFactory to {@link #createImageView(String, Object)}
     * <p>
     * Note - This method *SHOULD* be called before {@link #setImageUrls(List, List)}, {@link #setImageData(List)},{@link #setImageUrls(List)}
     *
     * @param glideImageViewFactory GlideImageViewFactory
     */
    public void setGlideImageViewFactory(GlideImageView.GlideImageViewFactory glideImageViewFactory) {
        mGlideImageViewFactory = glideImageViewFactory;
    }

    public void setImageUrls(List<String> urls) {
        setImageUrls(urls, null);
    }

    public void setImageUrls(List<String> urls, @Nullable List<?> tags) {
        if (tags != null && urls.size() != tags.size()) {
            throw new IllegalArgumentException("urls.size() != tags.size()");
        }

        final LinearLayout container = mContainer;
        container.removeAllViews();

        final int len = urls.size();
        if (tags == null) {
            for (int i = 0; i < len; i++) {
                addImageView(createImageView(urls.get(i), null));
            }
        } else {
            for (int i = 0; i < len; i++) {
                addImageView(createImageView(urls.get(i), tags.get(i)));
            }
        }
    }

    public void setImageData(List<? extends ImageData> objects) {
        ArrayList<String> urls = new ArrayList<>();
        ArrayList<Object> tags = new ArrayList<>();
        for (ImageData item : objects) {
            urls.add(item.getUrl());
            tags.add(item.getTag());
        }
        setImageUrls(urls, tags);
    }

    protected View createImageView(String url, Object tag) {
        return mGlideImageViewFactory.newImageView(getContext(), url, tag);
    }

    private void addImageView(View imageView) {
        if (mItemWidth == 0 && mItemHeight == 0) {
            mContainer.addView(imageView,
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        } else {
            mContainer.addView(imageView, new LinearLayout.LayoutParams(mItemWidth, mItemHeight));
        }
    }

    public interface ImageData {
        String getUrl();

        Object getTag();
    }

}
